package com.autosalon.ui.doplnek;

import com.autosalon.DAO.DaoFactory;
import com.autosalon.tools.IPaging;
import com.autosalon.tools.PagingComponent;
import com.autosalon.ui.ContentSwitcher;
import com.autosalon.ui.doplnek.action.BackButton;
import com.autosalon.ui.doplnek.action.DeleteDoplnekAction;
import com.autosalon.ui.doplnek.action.EditDoplnekAction;
import com.autosalon.ui.doplnek.action.NewDoplnekAction;
import com.autosalon.ui.doplnek.action.SaveDoplnekAction;
import com.autosalon.ui.doplnek.action.UpdateDoplneklAction;
import com.autosalon.ui.doplnek.state.DoplnekStateHolder;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import java.util.ArrayList;
import java.util.List;

/**
 * Nastaveni komponent pro doplnky
 * @author Petr Kriegisch
 */
public class Doplnek extends HorizontalLayout implements IPaging {

    private final ContentSwitcher switcher;
    private PagingComponent pagingComponent;
    private Table tabulka;
    private List<Object> doplnky;
    private com.autosalon.model.Doplnek dopl;
    private VerticalLayout vlMain;
    private Button btnNewDoplnek;
    private Button btnZpet;
    private Button btnSaveButton;
    private Button btnUpdate;
    private Form formNewDoplnek;
    private TextField tfNazevDoplnku;
    private TextField tfCenaDoplnku;
    int id;

    public Doplnek(ContentSwitcher switcher) {
        this.switcher = switcher;
        init();

    }

    /**
     * Inicializace komponent
     */
    private void init() {
        try {
            id = getSwitcher().getMainWindow().getZakaznik().getRole().getId().intValue();
        } catch (NullPointerException e) {
            id = 1;
        }
        btnNewDoplnek = new Button("Pridat novy doplnek");
        btnUpdate = new Button("Update");
        btnUpdate.addListener(new UpdateDoplneklAction(this));
        btnNewDoplnek.addListener(new NewDoplnekAction(this));
        vlMain = new VerticalLayout();
        pagingComponent = new PagingComponent(this);
        initTable();
        vlMain.setSpacing(true);

        vlMain.addComponent(pagingComponent);
        vlMain.addComponent(tabulka);
        vlMain.addComponent(btnNewDoplnek);
        vlMain.setComponentAlignment(tabulka, Alignment.MIDDLE_RIGHT);


        addComponent(vlMain);
        initFormNewDoplnek();

        DoplnekStateHolder.setState(DoplnekStateHolder.VIEW_DOPLNEK_STATE, id, this);
    }

    /**
     * Inicializace tabulky
     */
    private void initTable() {

        tabulka = new Table();
        tabulka.setCaption("Doplnky");

        tabulka.setSizeFull();
        tabulka.setColumnWidth("Nazev doplnku", 200);
        tabulka.setColumnWidth("Cena", 100);
        tabulka.setWidth("100%");
        tabulka.setHeight("200");

        IndexedContainer ic = new IndexedContainer();

        ic.addContainerProperty("Nazev doplnku", String.class, null);
        ic.addContainerProperty("Cena", Integer.class, null);
        if (id == 3) {
            ic.addContainerProperty("Upravit", Button.class, null);
            ic.addContainerProperty("Smazat", Button.class, null);
        }

        tabulka.setContainerDataSource(ic);
        naplnTabulkuObjednavky();
    }

    /**
     * Naplneni tabulky hodnotami
     */
    @Override
    public void naplnTabulkuObjednavky() {
        doplnky = new ArrayList<Object>();
        try {
            doplnky = DaoFactory.getinstance().getDoplnek().getAll(pagingComponent.getFrom(), pagingComponent.getTo());
            tabulka.setHeight("");
            tabulka.removeAllItems();

            for (Object ob : doplnky) {
                dopl = (com.autosalon.model.Doplnek) ob;

                if (id == 3) {
                    Button btnShowDoplnek;
                    btnShowDoplnek = new Button("Upravit");
                    btnShowDoplnek.setStyleName(Button.STYLE_LINK);
                    btnShowDoplnek.addListener(new EditDoplnekAction(this));
                    btnShowDoplnek.setData(dopl.getId());

                    Button btnDeleteDoplnek;
                    btnDeleteDoplnek = new Button("Smazat");
                    btnDeleteDoplnek.setStyleName(Button.STYLE_LINK);
                    btnDeleteDoplnek.addListener(new DeleteDoplnekAction(this));
                    btnDeleteDoplnek.setData(dopl.getId());
                    tabulka.addItem(new Object[]{dopl.getPopis_doplnku(), dopl.getCena(), btnShowDoplnek, btnDeleteDoplnek}, null);
                } else {
                }
                tabulka.addItem(new Object[]{dopl.getPopis_doplnku(), dopl.getCena()}, null);
            }
        } catch (Exception e) {
            getSwitcher().getWindow().showNotification("Pri mazani doplnku doslo k chybe");
        }





    }

    /**
     * Inicializace formulare pro novy doplnek
     */
    private void initFormNewDoplnek() {
        btnZpet = new Button("Zpet");
        btnZpet.addListener(new BackButton(this));
        tfNazevDoplnku = new TextField("Nazev doplnku");
        tfCenaDoplnku = new TextField("Cena doplnku");
        btnSaveButton = new Button("Vloz");
        btnSaveButton.addListener(new SaveDoplnekAction(this));
        formNewDoplnek = new Form();

        formNewDoplnek.setCaption("Novy doplnek");
        formNewDoplnek.setDescription("\n" + "Formular slouzi pro vkladani novych doplnku.");

        formNewDoplnek.getLayout().addComponent(tfNazevDoplnku);
        formNewDoplnek.getLayout().addComponent(tfCenaDoplnku);
        formNewDoplnek.getFooter().addComponent(btnSaveButton);
        formNewDoplnek.getFooter().addComponent(btnUpdate);
        formNewDoplnek.getFooter().addComponent(btnZpet);

        vlMain.addComponent(formNewDoplnek);

    }

    public ContentSwitcher getSwitcher() {
        return switcher;
    }

    public Table getTabulka() {
        return tabulka;
    }

    public void setTabulka(Table tabulka) {
        this.tabulka = tabulka;
    }

    public Button getBtnNewDoplnek() {
        return btnNewDoplnek;
    }

    public void setBtnNewDoplnek(Button btnNewDoplnek) {
        this.btnNewDoplnek = btnNewDoplnek;
    }

    public TextField getTfNazevDoplnku() {
        return tfNazevDoplnku;
    }

    public void setTfNazevDoplnku(TextField tfNazevDoplnku) {
        this.tfNazevDoplnku = tfNazevDoplnku;
    }

    public Form getFormNewDoplnek() {
        return formNewDoplnek;
    }

    public void setFormNewDoplnek(Form formNewDoplnek) {
        this.formNewDoplnek = formNewDoplnek;
    }

    public PagingComponent getPagingComponent() {
        return pagingComponent;
    }

    public void setPagingComponent(PagingComponent pagingComponent) {
        this.pagingComponent = pagingComponent;
    }

    public Button getBtnSaveButton() {
        return btnSaveButton;
    }

    public void setBtnSaveButton(Button btnSaveButton) {
        this.btnSaveButton = btnSaveButton;
    }

    public Button getBtnUpdate() {
        return btnUpdate;
    }

    public void setBtnUpdate(Button btnUpdate) {
        this.btnUpdate = btnUpdate;
    }

    public Button getBtnZpet() {
        return btnZpet;
    }

    public void setBtnZpet(Button btnZpet) {
        this.btnZpet = btnZpet;
    }

    public TextField getTfCenaDoplnku() {
        return tfCenaDoplnku;
    }

    public void setTfCenaDoplnku(TextField tfCenaDoplnku) {
        this.tfCenaDoplnku = tfCenaDoplnku;
    }
}
